#pragma once
#include "types.h"
#include <cmath>

int distance_sq(Vec2 a, Vec2 b) {
    return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}

bool detect_collision(const std::vector<Blob>& blobs) {
    for (size_t i = 0; i < blobs.size(); ++i) {
        for (size_t j = i + 1; j < blobs.size(); ++j) {
            int d = distance_sq(blobs[i].center, blobs[j].center);
            if (d < 100 && blobs[i].frames_static > 2 && blobs[j].frames_static > 2) {
                return true;
            }
        }
    }
    return false;
}

void update_static_frames(std::vector<Blob>& blobs) {
    for (auto& b : blobs) {
        int dx = std::abs(b.center.x - b.prev_center.x);
        int dy = std::abs(b.center.y - b.prev_center.y);
        if (dx + dy < 4)
            b.frames_static++;
        else
            b.frames_static = 0;
        b.prev_center = b.center;
    }
}